{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T09:00:40.969552Z",
     "start_time": "2019-10-29T09:00:38.275786Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from IPython.display import display\n",
    "pd.options.display.max_columns = 50"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.规划数据分析路线\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T05:51:52.125760Z",
     "start_time": "2019-10-29T05:51:51.944249Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>STABBR</th>\n",
       "      <th>HBCU</th>\n",
       "      <th>MENONLY</th>\n",
       "      <th>WOMENONLY</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>SATVRMID</th>\n",
       "      <th>SATMTMID</th>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <th>UGDS</th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <th>CURROPER</th>\n",
       "      <th>PCTPELL</th>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <th>UG25ABV</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama A &amp; M University</td>\n",
       "      <td>Normal</td>\n",
       "      <td>AL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>424.0</td>\n",
       "      <td>420.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4206.0</td>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "      <td>0.0656</td>\n",
       "      <td>1</td>\n",
       "      <td>0.7356</td>\n",
       "      <td>0.8284</td>\n",
       "      <td>0.1049</td>\n",
       "      <td>30300</td>\n",
       "      <td>33888</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>University of Alabama at Birmingham</td>\n",
       "      <td>Birmingham</td>\n",
       "      <td>AL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>570.0</td>\n",
       "      <td>565.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11383.0</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.2607</td>\n",
       "      <td>1</td>\n",
       "      <td>0.3460</td>\n",
       "      <td>0.5214</td>\n",
       "      <td>0.2422</td>\n",
       "      <td>39700</td>\n",
       "      <td>21941.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Amridge University</td>\n",
       "      <td>Montgomery</td>\n",
       "      <td>AL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>291.0</td>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "      <td>0.4536</td>\n",
       "      <td>1</td>\n",
       "      <td>0.6801</td>\n",
       "      <td>0.7795</td>\n",
       "      <td>0.8540</td>\n",
       "      <td>40100</td>\n",
       "      <td>23370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>University of Alabama in Huntsville</td>\n",
       "      <td>Huntsville</td>\n",
       "      <td>AL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>595.0</td>\n",
       "      <td>590.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5451.0</td>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "      <td>0.2146</td>\n",
       "      <td>1</td>\n",
       "      <td>0.3072</td>\n",
       "      <td>0.4596</td>\n",
       "      <td>0.2640</td>\n",
       "      <td>45500</td>\n",
       "      <td>24097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alabama State University</td>\n",
       "      <td>Montgomery</td>\n",
       "      <td>AL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>425.0</td>\n",
       "      <td>430.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4811.0</td>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "      <td>0.0892</td>\n",
       "      <td>1</td>\n",
       "      <td>0.7347</td>\n",
       "      <td>0.7554</td>\n",
       "      <td>0.1270</td>\n",
       "      <td>26600</td>\n",
       "      <td>33118.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                INSTNM        CITY STABBR  HBCU  MENONLY  \\\n",
       "0             Alabama A & M University      Normal     AL   1.0      0.0   \n",
       "1  University of Alabama at Birmingham  Birmingham     AL   0.0      0.0   \n",
       "2                   Amridge University  Montgomery     AL   0.0      0.0   \n",
       "3  University of Alabama in Huntsville  Huntsville     AL   0.0      0.0   \n",
       "4             Alabama State University  Montgomery     AL   1.0      0.0   \n",
       "\n",
       "   WOMENONLY  RELAFFIL  SATVRMID  SATMTMID  DISTANCEONLY     UGDS  UGDS_WHITE  \\\n",
       "0        0.0         0     424.0     420.0           0.0   4206.0      0.0333   \n",
       "1        0.0         0     570.0     565.0           0.0  11383.0      0.5922   \n",
       "2        0.0         1       NaN       NaN           1.0    291.0      0.2990   \n",
       "3        0.0         0     595.0     590.0           0.0   5451.0      0.6988   \n",
       "4        0.0         0     425.0     430.0           0.0   4811.0      0.0158   \n",
       "\n",
       "   UGDS_BLACK  UGDS_HISP  UGDS_ASIAN  UGDS_AIAN  UGDS_NHPI  UGDS_2MOR  \\\n",
       "0      0.9353     0.0055      0.0019     0.0024     0.0019     0.0000   \n",
       "1      0.2600     0.0283      0.0518     0.0022     0.0007     0.0368   \n",
       "2      0.4192     0.0069      0.0034     0.0000     0.0000     0.0000   \n",
       "3      0.1255     0.0382      0.0376     0.0143     0.0002     0.0172   \n",
       "4      0.9208     0.0121      0.0019     0.0010     0.0006     0.0098   \n",
       "\n",
       "   UGDS_NRA  UGDS_UNKN  PPTUG_EF  CURROPER  PCTPELL  PCTFLOAN  UG25ABV  \\\n",
       "0    0.0059     0.0138    0.0656         1   0.7356    0.8284   0.1049   \n",
       "1    0.0179     0.0100    0.2607         1   0.3460    0.5214   0.2422   \n",
       "2    0.0000     0.2715    0.4536         1   0.6801    0.7795   0.8540   \n",
       "3    0.0332     0.0350    0.2146         1   0.3072    0.4596   0.2640   \n",
       "4    0.0243     0.0137    0.0892         1   0.7347    0.7554   0.1270   \n",
       "\n",
       "  MD_EARN_WNE_P10 GRAD_DEBT_MDN_SUPP  \n",
       "0           30300              33888  \n",
       "1           39700            21941.5  \n",
       "2           40100              23370  \n",
       "3           45500              24097  \n",
       "4           26600            33118.5  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college = pd.read_csv('data/college.csv')\n",
    "college.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T05:52:32.836961Z",
     "start_time": "2019-10-29T05:52:32.830974Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7535, 27)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据的行数和列数\n",
    "college.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T05:54:52.917340Z",
     "start_time": "2019-10-29T05:54:52.799657Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.923291</td>\n",
       "      <td>0.266146</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count      mean       std  min     25%      50%       75%  max\n",
       "HBCU       7164.0  0.014238  0.118478  0.0  0.0000  0.00000  0.000000  1.0\n",
       "MENONLY    7164.0  0.009213  0.095546  0.0  0.0000  0.00000  0.000000  1.0\n",
       "WOMENONLY  7164.0  0.005304  0.072642  0.0  0.0000  0.00000  0.000000  1.0\n",
       "RELAFFIL   7535.0  0.190975  0.393096  0.0  0.0000  0.00000  0.000000  1.0\n",
       "...           ...       ...       ...  ...     ...      ...       ...  ...\n",
       "CURROPER   7535.0  0.923291  0.266146  0.0  1.0000  1.00000  1.000000  1.0\n",
       "PCTPELL    6849.0  0.530643  0.225544  0.0  0.3578  0.52150  0.712900  1.0\n",
       "PCTFLOAN   6849.0  0.522211  0.283616  0.0  0.3329  0.58330  0.745000  1.0\n",
       "UG25ABV    6718.0  0.410021  0.228939  0.0  0.2415  0.40075  0.572275  1.0\n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 统计数值列，并进行转置\n",
    "with pd.option_context('display.max_rows',8):\n",
    "    display(college.describe(include=[np.number]).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T05:57:45.708246Z",
     "start_time": "2019-10-29T05:57:45.590562Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.923291</td>\n",
       "      <td>0.266146</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count      mean       std  min     25%      50%       75%  max\n",
       "HBCU       7164.0  0.014238  0.118478  0.0  0.0000  0.00000  0.000000  1.0\n",
       "MENONLY    7164.0  0.009213  0.095546  0.0  0.0000  0.00000  0.000000  1.0\n",
       "WOMENONLY  7164.0  0.005304  0.072642  0.0  0.0000  0.00000  0.000000  1.0\n",
       "RELAFFIL   7535.0  0.190975  0.393096  0.0  0.0000  0.00000  0.000000  1.0\n",
       "...           ...       ...       ...  ...     ...      ...       ...  ...\n",
       "CURROPER   7535.0  0.923291  0.266146  0.0  1.0000  1.00000  1.000000  1.0\n",
       "PCTPELL    6849.0  0.530643  0.225544  0.0  0.3578  0.52150  0.712900  1.0\n",
       "PCTFLOAN   6849.0  0.522211  0.283616  0.0  0.3329  0.58330  0.745000  1.0\n",
       "UG25ABV    6718.0  0.410021  0.228939  0.0  0.2415  0.40075  0.572275  1.0\n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pd.options.display.max_rows = 8\n",
    "display(college.describe().T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T05:59:36.696422Z",
     "start_time": "2019-10-29T05:59:36.606663Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>unique</th>\n",
       "      <th>top</th>\n",
       "      <th>freq</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <td>7535</td>\n",
       "      <td>7535</td>\n",
       "      <td>Hartwick College</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CITY</th>\n",
       "      <td>7535</td>\n",
       "      <td>2514</td>\n",
       "      <td>New York</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <td>7535</td>\n",
       "      <td>59</td>\n",
       "      <td>CA</td>\n",
       "      <td>773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <td>6413</td>\n",
       "      <td>598</td>\n",
       "      <td>PrivacySuppressed</td>\n",
       "      <td>822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "      <td>7503</td>\n",
       "      <td>2038</td>\n",
       "      <td>PrivacySuppressed</td>\n",
       "      <td>1510</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   count unique                top  freq\n",
       "INSTNM              7535   7535   Hartwick College     1\n",
       "CITY                7535   2514           New York    87\n",
       "STABBR              7535     59                 CA   773\n",
       "MD_EARN_WNE_P10     6413    598  PrivacySuppressed   822\n",
       "GRAD_DEBT_MDN_SUPP  7503   2038  PrivacySuppressed  1510"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计对象和类型列\n",
    "college.describe(include=[np.object, pd.Categorical]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:00:42.521958Z",
     "start_time": "2019-10-29T06:00:42.500015Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7535 entries, 0 to 7534\n",
      "Data columns (total 27 columns):\n",
      "INSTNM                7535 non-null object\n",
      "CITY                  7535 non-null object\n",
      "STABBR                7535 non-null object\n",
      "HBCU                  7164 non-null float64\n",
      "MENONLY               7164 non-null float64\n",
      "WOMENONLY             7164 non-null float64\n",
      "RELAFFIL              7535 non-null int64\n",
      "SATVRMID              1185 non-null float64\n",
      "SATMTMID              1196 non-null float64\n",
      "DISTANCEONLY          7164 non-null float64\n",
      "UGDS                  6874 non-null float64\n",
      "UGDS_WHITE            6874 non-null float64\n",
      "UGDS_BLACK            6874 non-null float64\n",
      "UGDS_HISP             6874 non-null float64\n",
      "UGDS_ASIAN            6874 non-null float64\n",
      "UGDS_AIAN             6874 non-null float64\n",
      "UGDS_NHPI             6874 non-null float64\n",
      "UGDS_2MOR             6874 non-null float64\n",
      "UGDS_NRA              6874 non-null float64\n",
      "UGDS_UNKN             6874 non-null float64\n",
      "PPTUG_EF              6853 non-null float64\n",
      "CURROPER              7535 non-null int64\n",
      "PCTPELL               6849 non-null float64\n",
      "PCTFLOAN              6849 non-null float64\n",
      "UG25ABV               6718 non-null float64\n",
      "MD_EARN_WNE_P10       6413 non-null object\n",
      "GRAD_DEBT_MDN_SUPP    7503 non-null object\n",
      "dtypes: float64(20), int64(2), object(5)\n",
      "memory usage: 1.6+ MB\n"
     ]
    }
   ],
   "source": [
    "# 列出每列的数据类型，非缺失值的数量，以及内存的使用\n",
    "college.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:03:38.548206Z",
     "start_time": "2019-10-29T06:03:38.428560Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.923291</td>\n",
       "      <td>0.266146</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count      mean       std  min     25%      50%       75%  max\n",
       "HBCU       7164.0  0.014238  0.118478  0.0  0.0000  0.00000  0.000000  1.0\n",
       "MENONLY    7164.0  0.009213  0.095546  0.0  0.0000  0.00000  0.000000  1.0\n",
       "WOMENONLY  7164.0  0.005304  0.072642  0.0  0.0000  0.00000  0.000000  1.0\n",
       "RELAFFIL   7535.0  0.190975  0.393096  0.0  0.0000  0.00000  0.000000  1.0\n",
       "...           ...       ...       ...  ...     ...      ...       ...  ...\n",
       "CURROPER   7535.0  0.923291  0.266146  0.0  1.0000  1.00000  1.000000  1.0\n",
       "PCTPELL    6849.0  0.530643  0.225544  0.0  0.3578  0.52150  0.712900  1.0\n",
       "PCTFLOAN   6849.0  0.522211  0.283616  0.0  0.3329  0.58330  0.745000  1.0\n",
       "UG25ABV    6718.0  0.410021  0.228939  0.0  0.2415  0.40075  0.572275  1.0\n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=[np.number]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:04:51.054333Z",
     "start_time": "2019-10-29T06:04:50.992470Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>unique</th>\n",
       "      <th>top</th>\n",
       "      <th>freq</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <td>7535</td>\n",
       "      <td>7535</td>\n",
       "      <td>Hartwick College</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CITY</th>\n",
       "      <td>7535</td>\n",
       "      <td>2514</td>\n",
       "      <td>New York</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <td>7535</td>\n",
       "      <td>59</td>\n",
       "      <td>CA</td>\n",
       "      <td>773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <td>6413</td>\n",
       "      <td>598</td>\n",
       "      <td>PrivacySuppressed</td>\n",
       "      <td>822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "      <td>7503</td>\n",
       "      <td>2038</td>\n",
       "      <td>PrivacySuppressed</td>\n",
       "      <td>1510</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   count unique                top  freq\n",
       "INSTNM              7535   7535   Hartwick College     1\n",
       "CITY                7535   2514           New York    87\n",
       "STABBR              7535     59                 CA   773\n",
       "MD_EARN_WNE_P10     6413    598  PrivacySuppressed   822\n",
       "GRAD_DEBT_MDN_SUPP  7503   2038  PrivacySuppressed  1510"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=[np.object, pd.Categorical]).T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:08:05.888185Z",
     "start_time": "2019-10-29T06:08:05.758532Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>1%</th>\n",
       "      <th>5%</th>\n",
       "      <th>10%</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>99%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.986368</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>0.0374</td>\n",
       "      <td>0.0899</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.917383</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           count      mean       std  min      1%      5%     10%     25%  \\\n",
       "HBCU      7164.0  0.014238  0.118478  0.0  0.0000  0.0000  0.0000  0.0000   \n",
       "MENONLY   7164.0  0.009213  0.095546  0.0  0.0000  0.0000  0.0000  0.0000   \n",
       "...          ...       ...       ...  ...     ...     ...     ...     ...   \n",
       "PCTFLOAN  6849.0  0.522211  0.283616  0.0  0.0000  0.0000  0.0000  0.3329   \n",
       "UG25ABV   6718.0  0.410021  0.228939  0.0  0.0025  0.0374  0.0899  0.2415   \n",
       "\n",
       "              50%       99%  max  \n",
       "HBCU      0.00000  1.000000  1.0  \n",
       "MENONLY   0.00000  0.000000  1.0  \n",
       "...           ...       ...  ...  \n",
       "PCTFLOAN  0.58330  0.986368  1.0  \n",
       "UG25ABV   0.40075  0.917383  1.0  \n",
       "\n",
       "[22 rows x 11 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 在describe方法中，打印分位数\n",
    "with pd.option_context('display.max_rows', 5):\n",
    "    display(college.describe(include=[np.number],\n",
    "                            percentiles=[.01, .05, .10, .25, .99]).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:10:08.184128Z",
     "start_time": "2019-10-29T06:10:08.158231Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column_name</th>\n",
       "      <th>description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>INSTNM</td>\n",
       "      <td>Institution Name</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CITY</td>\n",
       "      <td>City Location</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>STABBR</td>\n",
       "      <td>State Abbreviation</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HBCU</td>\n",
       "      <td>Historically Black College or University</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>PCTFLOAN</td>\n",
       "      <td>Percent Students with federal loan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>UG25ABV</td>\n",
       "      <td>Percent Students Older than 25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>MD_EARN_WNE_P10</td>\n",
       "      <td>Median Earnings 10 years after enrollment</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>GRAD_DEBT_MDN_SUPP</td>\n",
       "      <td>Median debt of completers</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>27 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           column_name                                description\n",
       "0               INSTNM                           Institution Name\n",
       "1                 CITY                              City Location\n",
       "2               STABBR                         State Abbreviation\n",
       "3                 HBCU   Historically Black College or University\n",
       "..                 ...                                        ...\n",
       "23            PCTFLOAN         Percent Students with federal loan\n",
       "24             UG25ABV             Percent Students Older than 25\n",
       "25     MD_EARN_WNE_P10  Median Earnings 10 years after enrollment\n",
       "26  GRAD_DEBT_MDN_SUPP                  Median debt of completers\n",
       "\n",
       "[27 rows x 2 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 展示一个数据字典：数据字典的主要作用是解释列名的意义\n",
    "college_dd = pd.read_csv('data/college_data_dictionary.csv')\n",
    "with pd.option_context('display.max_rows', 8):\n",
    "    display(college_dd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.改变数据类型，降低内存消耗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:13:04.750934Z",
     "start_time": "2019-10-29T06:13:04.653232Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>SATMTMID</th>\n",
       "      <th>CURROPER</th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>STABBR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Alabama A &amp; M University</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>565.0</td>\n",
       "      <td>1</td>\n",
       "      <td>University of Alabama at Birmingham</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>Amridge University</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>590.0</td>\n",
       "      <td>1</td>\n",
       "      <td>University of Alabama in Huntsville</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>430.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Alabama State University</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   RELAFFIL  SATMTMID  CURROPER                               INSTNM STABBR\n",
       "0         0     420.0         1             Alabama A & M University     AL\n",
       "1         0     565.0         1  University of Alabama at Birmingham     AL\n",
       "2         1       NaN         1                   Amridge University     AL\n",
       "3         0     590.0         1  University of Alabama in Huntsville     AL\n",
       "4         0     430.0         1             Alabama State University     AL"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取五列\n",
    "college = pd.read_csv('data/college.csv')\n",
    "different_cols = ['RELAFFIL', 'SATMTMID', 'CURROPER', 'INSTNM', 'STABBR']\n",
    "col2 = college.loc[:, different_cols]\n",
    "col2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:13:55.205005Z",
     "start_time": "2019-10-29T06:13:55.198025Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RELAFFIL      int64\n",
       "SATMTMID    float64\n",
       "CURROPER      int64\n",
       "INSTNM       object\n",
       "STABBR       object\n",
       "dtype: object"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数据类型\n",
    "col2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:40:15.618852Z",
     "start_time": "2019-10-29T06:40:15.597908Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index             80\n",
       "RELAFFIL       60280\n",
       "SATMTMID       60280\n",
       "CURROPER       60280\n",
       "INSTNM        660699\n",
       "STABBR         13576\n",
       "RELAFFILEL      7535\n",
       "dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用memory_usage方法查看每列的内存消耗\n",
    "original_mem = col2.memory_usage(deep=True)\n",
    "original_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:17:54.883034Z",
     "start_time": "2019-10-29T06:17:54.873060Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RELAFFIL        int64\n",
       "SATMTMID      float64\n",
       "CURROPER        int64\n",
       "INSTNM         object\n",
       "STABBR         object\n",
       "RELAFFILEL       int8\n",
       "dtype: object"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# RELAFFIL这列只包含0或1，因此没必要用64位，使用astype方法将其变为8位（1字节）整数\n",
    "col2['RELAFFILEL'] = col2['RELAFFIL'].astype(np.int8)\n",
    "# 再次查看数据类型\n",
    "col2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:19:35.270599Z",
     "start_time": "2019-10-29T06:19:35.231671Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "INSTNM    7535\n",
       "STABBR      59\n",
       "dtype: int64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查两个对象列的独立值的个数\n",
    "col2.select_dtypes(include=['object']).nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:37:17.717736Z",
     "start_time": "2019-10-29T06:37:17.703773Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RELAFFIL         int64\n",
       "SATMTMID       float64\n",
       "CURROPER         int64\n",
       "INSTNM          object\n",
       "STABBR        category\n",
       "RELAFFILEL        int8\n",
       "dtype: object"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# STABBR列可以转变为“类型”（Categorical），独立值的个数小于总数的1%\n",
    "col2['STABBR'] = col2['STABBR'].astype('category')\n",
    "col2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:38:07.726997Z",
     "start_time": "2019-10-29T06:38:07.711041Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index             80\n",
       "RELAFFIL       60280\n",
       "SATMTMID       60280\n",
       "CURROPER       60280\n",
       "INSTNM        660699\n",
       "STABBR         13576\n",
       "RELAFFILEL      7535\n",
       "dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 再次检查内存的使用\n",
    "new_mem = col2.memory_usage(deep=True)\n",
    "new_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:40:19.653100Z",
     "start_time": "2019-10-29T06:40:19.645089Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index         1.0\n",
       "RELAFFIL      1.0\n",
       "SATMTMID      1.0\n",
       "CURROPER      1.0\n",
       "INSTNM        1.0\n",
       "STABBR        1.0\n",
       "RELAFFILEL    1.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过和原始数据比较，RELAFFIL列变为了原来的八分之一，STABBR列只有原始大小的3%\n",
    "new_mem / original_mem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:44:40.354478Z",
     "start_time": "2019-10-29T06:44:40.263719Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index           80\n",
       "CURROPER     60280\n",
       "INSTNM      660240\n",
       "dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# CURROPER和INSTNM分别是int64和对象类型\n",
    "college = pd.read_csv('data/college.csv')\n",
    "college[['CURROPER', 'INSTNM']].memory_usage(deep=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:48:28.859331Z",
     "start_time": "2019-10-29T06:48:28.830419Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index           80\n",
       "CURROPER     60280\n",
       "INSTNM      660380\n",
       "dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# CURROPER列加上了10000000，但是内存使用没有变化；但是INSTNM列加上了一个a，内存消耗增加了105字节\n",
    "college.loc[0, 'CURROPER'] = 10000000\n",
    "college.loc[0, 'INSTNM'] = college.loc[0, 'INSTNM'] + 'a'\n",
    "# college.loc[1, 'INSTNM'] = college.loc[1, 'INSTNM'] +'a'\n",
    "college[['CURROPER', 'INSTNM']].memory_usage(deep=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:49:14.625731Z",
     "start_time": "2019-10-29T06:49:14.615757Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据字典中的信息显示MENONLY这列只包含0和1，但是由于含有缺失值，它的类型是浮点型\n",
    "college['MENONLY'].dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:49:50.621472Z",
     "start_time": "2019-10-29T06:49:50.581576Z"
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Cannot convert non-finite values (NA or inf) to integer",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-36-9172f873e227>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 任何数值类型的列，只要有一个缺失值，就会成为浮点型；这列中的任何整数都会强制成为浮点型\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mcollege\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'MENONLY'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'int8'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors, **kwargs)\u001b[0m\n\u001b[0;32m   5689\u001b[0m             \u001b[1;31m# else, only a single dtype is given\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5690\u001b[0m             new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,\n\u001b[1;32m-> 5691\u001b[1;33m                                          **kwargs)\n\u001b[0m\u001b[0;32m   5692\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_data\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__finalize__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5693\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, **kwargs)\u001b[0m\n\u001b[0;32m    529\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    530\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 531\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'astype'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    532\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    533\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mapply\u001b[1;34m(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)\u001b[0m\n\u001b[0;32m    393\u001b[0m                                             copy=align_copy)\n\u001b[0;32m    394\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 395\u001b[1;33m             \u001b[0mapplied\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    396\u001b[0m             \u001b[0mresult_blocks\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_extend_blocks\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mapplied\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult_blocks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    397\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\blocks.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors, values, **kwargs)\u001b[0m\n\u001b[0;32m    532\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'raise'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    533\u001b[0m         return self._astype(dtype, copy=copy, errors=errors, values=values,\n\u001b[1;32m--> 534\u001b[1;33m                             **kwargs)\n\u001b[0m\u001b[0;32m    535\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    536\u001b[0m     def _astype(self, dtype, copy=False, errors='raise', values=None,\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\blocks.py\u001b[0m in \u001b[0;36m_astype\u001b[1;34m(self, dtype, copy, errors, values, **kwargs)\u001b[0m\n\u001b[0;32m    631\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    632\u001b[0m                     \u001b[1;31m# _astype_nansafe works fine with 1-d only\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 633\u001b[1;33m                     \u001b[0mvalues\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mastype_nansafe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    634\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    635\u001b[0m                 \u001b[1;31m# TODO(extension)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\dtypes\\cast.py\u001b[0m in \u001b[0;36mastype_nansafe\u001b[1;34m(arr, dtype, copy, skipna)\u001b[0m\n\u001b[0;32m    674\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    675\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misfinite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 676\u001b[1;33m             raise ValueError('Cannot convert non-finite values (NA or inf) to '\n\u001b[0m\u001b[0;32m    677\u001b[0m                              'integer')\n\u001b[0;32m    678\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Cannot convert non-finite values (NA or inf) to integer"
     ]
    }
   ],
   "source": [
    "# 任何数值类型的列，只要有一个缺失值，就会成为浮点型；这列中的任何整数都会强制成为浮点型\n",
    "college['MENONLY'].astype('int8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对于数据类型，可以替换字符串名\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:53:06.425828Z",
     "start_time": "2019-10-29T06:53:06.322107Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>1328.063172</td>\n",
       "      <td>115201.552429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count         mean            std  min     25%      50%       75%  \\\n",
       "HBCU       7164.0     0.014238       0.118478  0.0  0.0000  0.00000  0.000000   \n",
       "MENONLY    7164.0     0.009213       0.095546  0.0  0.0000  0.00000  0.000000   \n",
       "WOMENONLY  7164.0     0.005304       0.072642  0.0  0.0000  0.00000  0.000000   \n",
       "RELAFFIL   7535.0     0.190975       0.393096  0.0  0.0000  0.00000  0.000000   \n",
       "...           ...          ...            ...  ...     ...      ...       ...   \n",
       "CURROPER   7535.0  1328.063172  115201.552429  0.0  1.0000  1.00000  1.000000   \n",
       "PCTPELL    6849.0     0.530643       0.225544  0.0  0.3578  0.52150  0.712900   \n",
       "PCTFLOAN   6849.0     0.522211       0.283616  0.0  0.3329  0.58330  0.745000   \n",
       "UG25ABV    6718.0     0.410021       0.228939  0.0  0.2415  0.40075  0.572275   \n",
       "\n",
       "                  max  \n",
       "HBCU              1.0  \n",
       "MENONLY           1.0  \n",
       "WOMENONLY         1.0  \n",
       "RELAFFIL          1.0  \n",
       "...               ...  \n",
       "CURROPER   10000000.0  \n",
       "PCTPELL           1.0  \n",
       "PCTFLOAN          1.0  \n",
       "UG25ABV           1.0  \n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=['int64', 'float64']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:53:58.504073Z",
     "start_time": "2019-10-29T06:53:58.388381Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>1328.063172</td>\n",
       "      <td>115201.552429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count         mean            std  min     25%      50%       75%  \\\n",
       "HBCU       7164.0     0.014238       0.118478  0.0  0.0000  0.00000  0.000000   \n",
       "MENONLY    7164.0     0.009213       0.095546  0.0  0.0000  0.00000  0.000000   \n",
       "WOMENONLY  7164.0     0.005304       0.072642  0.0  0.0000  0.00000  0.000000   \n",
       "RELAFFIL   7535.0     0.190975       0.393096  0.0  0.0000  0.00000  0.000000   \n",
       "...           ...          ...            ...  ...     ...      ...       ...   \n",
       "CURROPER   7535.0  1328.063172  115201.552429  0.0  1.0000  1.00000  1.000000   \n",
       "PCTPELL    6849.0     0.530643       0.225544  0.0  0.3578  0.52150  0.712900   \n",
       "PCTFLOAN   6849.0     0.522211       0.283616  0.0  0.3329  0.58330  0.745000   \n",
       "UG25ABV    6718.0     0.410021       0.228939  0.0  0.2415  0.40075  0.572275   \n",
       "\n",
       "                  max  \n",
       "HBCU              1.0  \n",
       "MENONLY           1.0  \n",
       "WOMENONLY         1.0  \n",
       "RELAFFIL          1.0  \n",
       "...               ...  \n",
       "CURROPER   10000000.0  \n",
       "PCTPELL           1.0  \n",
       "PCTFLOAN          1.0  \n",
       "UG25ABV           1.0  \n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=[np.int64, np.float64]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:56:55.356864Z",
     "start_time": "2019-10-29T06:56:55.213248Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATVRMID</th>\n",
       "      <td>1185.0</td>\n",
       "      <td>522.819409</td>\n",
       "      <td>68.578862</td>\n",
       "      <td>290.0</td>\n",
       "      <td>475.0000</td>\n",
       "      <td>510.00000</td>\n",
       "      <td>555.000000</td>\n",
       "      <td>765.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <td>6853.0</td>\n",
       "      <td>0.226639</td>\n",
       "      <td>0.246470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.15040</td>\n",
       "      <td>0.376900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>20 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count        mean        std    min       25%        50%  \\\n",
       "HBCU       7164.0    0.014238   0.118478    0.0    0.0000    0.00000   \n",
       "MENONLY    7164.0    0.009213   0.095546    0.0    0.0000    0.00000   \n",
       "WOMENONLY  7164.0    0.005304   0.072642    0.0    0.0000    0.00000   \n",
       "SATVRMID   1185.0  522.819409  68.578862  290.0  475.0000  510.00000   \n",
       "...           ...         ...        ...    ...       ...        ...   \n",
       "PPTUG_EF   6853.0    0.226639   0.246470    0.0    0.0000    0.15040   \n",
       "PCTPELL    6849.0    0.530643   0.225544    0.0    0.3578    0.52150   \n",
       "PCTFLOAN   6849.0    0.522211   0.283616    0.0    0.3329    0.58330   \n",
       "UG25ABV    6718.0    0.410021   0.228939    0.0    0.2415    0.40075   \n",
       "\n",
       "                  75%    max  \n",
       "HBCU         0.000000    1.0  \n",
       "MENONLY      0.000000    1.0  \n",
       "WOMENONLY    0.000000    1.0  \n",
       "SATVRMID   555.000000  765.0  \n",
       "...               ...    ...  \n",
       "PPTUG_EF     0.376900    1.0  \n",
       "PCTPELL      0.712900    1.0  \n",
       "PCTFLOAN     0.745000    1.0  \n",
       "UG25ABV      0.572275    1.0  \n",
       "\n",
       "[20 rows x 8 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college['RELAFFIL'] = college['RELAFFIL'].astype(np.int8)\n",
    "college.describe(include=['int', 'float']).T # defaults to 64 bit int/floats\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:57:26.260219Z",
     "start_time": "2019-10-29T06:57:26.116603Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>1328.063172</td>\n",
       "      <td>115201.552429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count         mean            std  min     25%      50%       75%  \\\n",
       "HBCU       7164.0     0.014238       0.118478  0.0  0.0000  0.00000  0.000000   \n",
       "MENONLY    7164.0     0.009213       0.095546  0.0  0.0000  0.00000  0.000000   \n",
       "WOMENONLY  7164.0     0.005304       0.072642  0.0  0.0000  0.00000  0.000000   \n",
       "RELAFFIL   7535.0     0.190975       0.393096  0.0  0.0000  0.00000  0.000000   \n",
       "...           ...          ...            ...  ...     ...      ...       ...   \n",
       "CURROPER   7535.0  1328.063172  115201.552429  0.0  1.0000  1.00000  1.000000   \n",
       "PCTPELL    6849.0     0.530643       0.225544  0.0  0.3578  0.52150  0.712900   \n",
       "PCTFLOAN   6849.0     0.522211       0.283616  0.0  0.3329  0.58330  0.745000   \n",
       "UG25ABV    6718.0     0.410021       0.228939  0.0  0.2415  0.40075  0.572275   \n",
       "\n",
       "                  max  \n",
       "HBCU              1.0  \n",
       "MENONLY           1.0  \n",
       "WOMENONLY         1.0  \n",
       "RELAFFIL          1.0  \n",
       "...               ...  \n",
       "CURROPER   10000000.0  \n",
       "PCTPELL           1.0  \n",
       "PCTFLOAN          1.0  \n",
       "UG25ABV           1.0  \n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=['number']).T # also works asthe default int/float are 64 bits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T06:58:43.062805Z",
     "start_time": "2019-10-29T06:58:43.052867Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60280"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转变数据类型时也可以如法炮制\n",
    "In[32]: college['MENONLY'] = college['MENONLY'].astype('float16')\n",
    "college['RELAFFIL'] = college['RELAFFIL'].astype('int8')\n",
    "college.index = pd.Int64Index(college.index)\n",
    "college.index.memory_usage()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.从最大中选择最小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:00:55.481707Z",
     "start_time": "2019-10-29T07:00:55.396907Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>7.9</td>\n",
       "      <td>237000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>7.1</td>\n",
       "      <td>300000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>6.8</td>\n",
       "      <td>245000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>8.5</td>\n",
       "      <td>250000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>7.1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title  imdb_score       budget\n",
       "0                                      Avatar         7.9  237000000.0\n",
       "1    Pirates of the Caribbean: At World's End         7.1  300000000.0\n",
       "2                                     Spectre         6.8  245000000.0\n",
       "3                       The Dark Knight Rises         8.5  250000000.0\n",
       "4  Star Wars: Episode VII - The Force Awakens         7.1          NaN"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取movie.csv，选取'movie_title', 'imdb_score', 'budget'三列\n",
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie2 = movie[['movie_title', 'imdb_score', 'budget']]\n",
    "movie2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:02:40.582641Z",
     "start_time": "2019-10-29T07:02:40.564652Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2725</th>\n",
       "      <td>Towering Inferno</td>\n",
       "      <td>9.5</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1920</th>\n",
       "      <td>The Shawshank Redemption</td>\n",
       "      <td>9.3</td>\n",
       "      <td>25000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3402</th>\n",
       "      <td>The Godfather</td>\n",
       "      <td>9.2</td>\n",
       "      <td>6000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2779</th>\n",
       "      <td>Dekalog</td>\n",
       "      <td>9.1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>9.1</td>\n",
       "      <td>17000000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   movie_title  imdb_score      budget\n",
       "2725          Towering Inferno         9.5         NaN\n",
       "1920  The Shawshank Redemption         9.3  25000000.0\n",
       "3402             The Godfather         9.2   6000000.0\n",
       "2779                   Dekalog         9.1         NaN\n",
       "4312      Kickboxer: Vengeance         9.1  17000000.0"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用nlargest方法，选出imdb_score分数最高的100个\n",
    "movie2.nlargest(100, 'imdb_score').head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:04:17.789644Z",
     "start_time": "2019-10-29T07:04:17.764743Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4804</th>\n",
       "      <td>Butterfly Girl</td>\n",
       "      <td>8.7</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4801</th>\n",
       "      <td>Children of Heaven</td>\n",
       "      <td>8.5</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>12 Angry Men</td>\n",
       "      <td>8.9</td>\n",
       "      <td>350000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4550</th>\n",
       "      <td>A Separation</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               movie_title  imdb_score    budget\n",
       "4804        Butterfly Girl         8.7  180000.0\n",
       "4801    Children of Heaven         8.5  180000.0\n",
       "4706          12 Angry Men         8.9  350000.0\n",
       "4550          A Separation         8.4  500000.0\n",
       "4636  The Other Dream Team         8.4  500000.0"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用链式操作，nsmallest方法再从中挑出预算最小的五部\n",
    "movie2.nlargest(100, 'imdb_score').nsmallest(5, 'budget')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.通过排序选取每组的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T09:00:47.746426Z",
     "start_time": "2019-10-29T09:00:47.545962Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>imdb_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3884</th>\n",
       "      <td>The Veil</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>4.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2375</th>\n",
       "      <td>My Big Fat Greek Wedding 2</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>6.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2794</th>\n",
       "      <td>Miracles from Heaven</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>6.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>Independence Day: Resurgence</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>5.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>153</th>\n",
       "      <td>Kung Fu Panda 3</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>7.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       movie_title  title_year  imdb_score\n",
       "3884                      The Veil      2016.0         4.7\n",
       "2375    My Big Fat Greek Wedding 2      2016.0         6.1\n",
       "2794          Miracles from Heaven      2016.0         6.8\n",
       "92    Independence Day: Resurgence      2016.0         5.5\n",
       "153                Kung Fu Panda 3      2016.0         7.2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同上，选取出三列。按照title_year降序排列\n",
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie2 = movie[['movie_title', 'title_year', 'imdb_score']]\n",
    "movie2.sort_values('title_year', ascending=False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T09:02:38.144223Z",
     "start_time": "2019-10-29T09:02:38.115265Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>imdb_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>9.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4277</th>\n",
       "      <td>A Beginner's Guide to Snuff</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3798</th>\n",
       "      <td>Airlift</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Captain America: Civil War</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Godzilla Resurgence</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      movie_title  title_year  imdb_score\n",
       "4312         Kickboxer: Vengeance      2016.0         9.1\n",
       "4277  A Beginner's Guide to Snuff      2016.0         8.7\n",
       "3798                      Airlift      2016.0         8.5\n",
       "27     Captain America: Civil War      2016.0         8.2\n",
       "98            Godzilla Resurgence      2016.0         8.2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用列表同时对两列进行排序\n",
    "movie3 = movie2.sort_values(['title_year', 'imdb_score'], ascending=False)\n",
    "movie3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:23:00.504067Z",
     "start_time": "2019-10-30T02:23:00.480165Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>imdb_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>9.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3745</th>\n",
       "      <td>Running Forever</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>8.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4369</th>\n",
       "      <td>Queen of the Mountains</td>\n",
       "      <td>2014.0</td>\n",
       "      <td>8.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3935</th>\n",
       "      <td>Batman: The Dark Knight Returns, Part 2</td>\n",
       "      <td>2013.0</td>\n",
       "      <td>8.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title  title_year  imdb_score\n",
       "4312                     Kickboxer: Vengeance      2016.0         9.1\n",
       "3745                          Running Forever      2015.0         8.6\n",
       "4369                   Queen of the Mountains      2014.0         8.7\n",
       "3935  Batman: The Dark Knight Returns, Part 2      2013.0         8.4\n",
       "3                       The Dark Knight Rises      2012.0         8.5"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop_duplicates去重，只保留每年的第一条数据\n",
    "movie_top_year = movie3.drop_duplicates(subset='title_year')\n",
    "movie_top_year.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:30:04.857255Z",
     "start_time": "2019-10-30T02:30:04.811376Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>content_rating</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4026</th>\n",
       "      <td>Compadres</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>R</td>\n",
       "      <td>3000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4658</th>\n",
       "      <td>Fight to the Finish</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>150000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4661</th>\n",
       "      <td>Rodeo Girl</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>PG</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3252</th>\n",
       "      <td>The Wailing</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>Not Rated</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4659</th>\n",
       "      <td>Alleluia! The Devil's Carnival</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4731</th>\n",
       "      <td>Bizarre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>Unrated</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>812</th>\n",
       "      <td>The Ridiculous 6</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>TV-14</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4831</th>\n",
       "      <td>The Gallows</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>R</td>\n",
       "      <td>100000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4825</th>\n",
       "      <td>Romantic Schemer</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>125000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3796</th>\n",
       "      <td>R.L. Stine's Monsterville: The Cabinet of Souls</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG</td>\n",
       "      <td>4400000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          movie_title  title_year  \\\n",
       "4026                                        Compadres      2016.0   \n",
       "4658                              Fight to the Finish      2016.0   \n",
       "4661                                       Rodeo Girl      2016.0   \n",
       "3252                                      The Wailing      2016.0   \n",
       "4659                   Alleluia! The Devil's Carnival      2016.0   \n",
       "4731                                          Bizarre      2015.0   \n",
       "812                                  The Ridiculous 6      2015.0   \n",
       "4831                                      The Gallows      2015.0   \n",
       "4825                                 Romantic Schemer      2015.0   \n",
       "3796  R.L. Stine's Monsterville: The Cabinet of Souls      2015.0   \n",
       "\n",
       "     content_rating     budget  \n",
       "4026              R  3000000.0  \n",
       "4658          PG-13   150000.0  \n",
       "4661             PG   500000.0  \n",
       "3252      Not Rated        NaN  \n",
       "4659            NaN   500000.0  \n",
       "4731        Unrated   500000.0  \n",
       "812           TV-14        NaN  \n",
       "4831              R   100000.0  \n",
       "4825          PG-13   125000.0  \n",
       "3796             PG  4400000.0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过给ascending设置列表，可以同时对一列降序排列，一列升序排列\n",
    "movie4 = movie[['movie_title', 'title_year', 'content_rating', 'budget']]\n",
    "movie4_sorted = movie4.sort_values(['title_year', 'content_rating', 'budget'], ascending=[False, False, True])\n",
    "movie4_sorted.drop_duplicates(subset=['title_year', 'content_rating']).head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.用sort_values复现nlargest方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:36:53.280058Z",
     "start_time": "2019-10-30T02:36:53.173343Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4804</th>\n",
       "      <td>Butterfly Girl</td>\n",
       "      <td>8.7</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4801</th>\n",
       "      <td>Children of Heaven</td>\n",
       "      <td>8.5</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>12 Angry Men</td>\n",
       "      <td>8.9</td>\n",
       "      <td>350000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4550</th>\n",
       "      <td>A Separation</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               movie_title  imdb_score    budget\n",
       "4804        Butterfly Girl         8.7  180000.0\n",
       "4801    Children of Heaven         8.5  180000.0\n",
       "4706          12 Angry Men         8.9  350000.0\n",
       "4550          A Separation         8.4  500000.0\n",
       "4636  The Other Dream Team         8.4  500000.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 和前面一样nlargest和nsmallest链式操作进行选取\n",
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie2 = movie[['movie_title', 'imdb_score', 'budget']]\n",
    "movie_smallest_largest = movie2.nlargest(100, 'imdb_score').nsmallest(5, 'budget')\n",
    "movie_smallest_largest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:42:38.245869Z",
     "start_time": "2019-10-30T02:42:38.227947Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4815</th>\n",
       "      <td>A Charlie Brown Christmas</td>\n",
       "      <td>8.4</td>\n",
       "      <td>150000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4801</th>\n",
       "      <td>Children of Heaven</td>\n",
       "      <td>8.5</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4804</th>\n",
       "      <td>Butterfly Girl</td>\n",
       "      <td>8.7</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>12 Angry Men</td>\n",
       "      <td>8.9</td>\n",
       "      <td>350000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    movie_title  imdb_score    budget\n",
       "4815  A Charlie Brown Christmas         8.4  150000.0\n",
       "4801         Children of Heaven         8.5  180000.0\n",
       "4804             Butterfly Girl         8.7  180000.0\n",
       "4706               12 Angry Men         8.9  350000.0\n",
       "4636       The Other Dream Team         8.4  500000.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用sort_values方法，选取imdb_score最高的100个\n",
    "movie2.sort_values('imdb_score', ascending=False).head(100).sort_values('budget').head()\n",
    "# 然后可以再.sort_values('budget').head()，选出预算最低的5个，结果如下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 这两种方法得到的最小的5部电影不同，用tail进行调查："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:43:37.663854Z",
     "start_time": "2019-10-30T02:43:37.643908Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4023</th>\n",
       "      <td>Oldboy</td>\n",
       "      <td>8.4</td>\n",
       "      <td>3000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4163</th>\n",
       "      <td>To Kill a Mockingbird</td>\n",
       "      <td>8.4</td>\n",
       "      <td>2000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4395</th>\n",
       "      <td>Reservoir Dogs</td>\n",
       "      <td>8.4</td>\n",
       "      <td>1200000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4550</th>\n",
       "      <td>A Separation</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                movie_title  imdb_score     budget\n",
       "4023                 Oldboy         8.4  3000000.0\n",
       "4163  To Kill a Mockingbird         8.4  2000000.0\n",
       "4395         Reservoir Dogs         8.4  1200000.0\n",
       "4550           A Separation         8.4   500000.0\n",
       "4636   The Other Dream Team         8.4   500000.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.nlargest(100, 'imdb_score').tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:44:03.672331Z",
     "start_time": "2019-10-30T02:44:03.652386Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3799</th>\n",
       "      <td>Anne of Green Gables</td>\n",
       "      <td>8.4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3777</th>\n",
       "      <td>Requiem for a Dream</td>\n",
       "      <td>8.4</td>\n",
       "      <td>4500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3935</th>\n",
       "      <td>Batman: The Dark Knight Returns, Part 2</td>\n",
       "      <td>8.4</td>\n",
       "      <td>3500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2455</th>\n",
       "      <td>Aliens</td>\n",
       "      <td>8.4</td>\n",
       "      <td>18500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title  imdb_score      budget\n",
       "3799                     Anne of Green Gables         8.4         NaN\n",
       "3777                      Requiem for a Dream         8.4   4500000.0\n",
       "3935  Batman: The Dark Knight Returns, Part 2         8.4   3500000.0\n",
       "4636                     The Other Dream Team         8.4    500000.0\n",
       "2455                                   Aliens         8.4  18500000.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.sort_values('imdb_score', ascending=False).head(100).tail()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 这是因为评分在8.4以上的电影超过了100部。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.计算跟踪止损单价格"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### pip install pandas_datareader 或 conda install pandas_datareader，来安装pandas_datareader\n",
    "##### 笔记：pandas_datareader的问题 pandas_datareader在读取“google”源时会有\n",
    "##### 问题。如果碰到问题，切换到“Yahoo”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T02:57:49.890691Z",
     "start_time": "2019-10-30T02:57:48.301912Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas_datareader as pdr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T03:00:11.337308Z",
     "start_time": "2019-10-30T03:00:09.725587Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Open</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Adj Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-01-03</th>\n",
       "      <td>220.330002</td>\n",
       "      <td>210.960007</td>\n",
       "      <td>214.860001</td>\n",
       "      <td>216.990005</td>\n",
       "      <td>5923300</td>\n",
       "      <td>216.990005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-04</th>\n",
       "      <td>228.000000</td>\n",
       "      <td>214.309998</td>\n",
       "      <td>214.750000</td>\n",
       "      <td>226.990005</td>\n",
       "      <td>11213500</td>\n",
       "      <td>226.990005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-05</th>\n",
       "      <td>227.479996</td>\n",
       "      <td>221.949997</td>\n",
       "      <td>226.419998</td>\n",
       "      <td>226.750000</td>\n",
       "      <td>5911700</td>\n",
       "      <td>226.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-06</th>\n",
       "      <td>230.309998</td>\n",
       "      <td>225.449997</td>\n",
       "      <td>226.929993</td>\n",
       "      <td>229.009995</td>\n",
       "      <td>5527900</td>\n",
       "      <td>229.009995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-09</th>\n",
       "      <td>231.919998</td>\n",
       "      <td>228.000000</td>\n",
       "      <td>228.970001</td>\n",
       "      <td>231.279999</td>\n",
       "      <td>3979500</td>\n",
       "      <td>231.279999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-10</th>\n",
       "      <td>232.000000</td>\n",
       "      <td>226.889999</td>\n",
       "      <td>232.000000</td>\n",
       "      <td>229.869995</td>\n",
       "      <td>3660000</td>\n",
       "      <td>229.869995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-11</th>\n",
       "      <td>229.979996</td>\n",
       "      <td>226.679993</td>\n",
       "      <td>229.070007</td>\n",
       "      <td>229.729996</td>\n",
       "      <td>3650800</td>\n",
       "      <td>229.729996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-01-12</th>\n",
       "      <td>230.699997</td>\n",
       "      <td>225.580002</td>\n",
       "      <td>229.059998</td>\n",
       "      <td>229.589996</td>\n",
       "      <td>3790200</td>\n",
       "      <td>229.589996</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  High         Low        Open       Close    Volume  \\\n",
       "Date                                                                   \n",
       "2017-01-03  220.330002  210.960007  214.860001  216.990005   5923300   \n",
       "2017-01-04  228.000000  214.309998  214.750000  226.990005  11213500   \n",
       "2017-01-05  227.479996  221.949997  226.419998  226.750000   5911700   \n",
       "2017-01-06  230.309998  225.449997  226.929993  229.009995   5527900   \n",
       "2017-01-09  231.919998  228.000000  228.970001  231.279999   3979500   \n",
       "2017-01-10  232.000000  226.889999  232.000000  229.869995   3660000   \n",
       "2017-01-11  229.979996  226.679993  229.070007  229.729996   3650800   \n",
       "2017-01-12  230.699997  225.580002  229.059998  229.589996   3790200   \n",
       "\n",
       "             Adj Close  \n",
       "Date                    \n",
       "2017-01-03  216.990005  \n",
       "2017-01-04  226.990005  \n",
       "2017-01-05  226.750000  \n",
       "2017-01-06  229.009995  \n",
       "2017-01-09  231.279999  \n",
       "2017-01-10  229.869995  \n",
       "2017-01-11  229.729996  \n",
       "2017-01-12  229.589996  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询特斯拉在2017年第一天的股价\n",
    "tsla = pdr.DataReader('tsla', data_source='yahoo', start='2017-1-1')\n",
    "tsla.head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T03:02:41.845772Z",
     "start_time": "2019-10-30T03:02:41.836798Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-03    216.990005\n",
       "2017-01-04    226.990005\n",
       "2017-01-05    226.990005\n",
       "2017-01-06    229.009995\n",
       "2017-01-09    231.279999\n",
       "2017-01-10    231.279999\n",
       "2017-01-11    231.279999\n",
       "2017-01-12    231.279999\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla_close = tsla['Close']\n",
    "tsla_cummax = tsla_close.cummax()\n",
    "tsla_cummax.head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T03:03:09.686317Z",
     "start_time": "2019-10-30T03:03:09.677339Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-03    216.990005\n",
       "2017-01-04    226.990005\n",
       "2017-01-05    226.990005\n",
       "2017-01-06    229.009995\n",
       "2017-01-09    231.279999\n",
       "2017-01-10    231.279999\n",
       "2017-01-11    231.279999\n",
       "2017-01-12    231.279999\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla['Close'].cummax().head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T03:07:05.106330Z",
     "start_time": "2019-10-30T03:07:04.987646Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-01-03    195.291005\n",
       "2017-01-04    204.291005\n",
       "2017-01-05    204.291005\n",
       "2017-01-06    206.108995\n",
       "2017-01-09    208.151999\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将下行区间限制到10%，将tsla_cummax乘以0.9\n",
    "tsla_trailing_stop = tsla_cummax * .9\n",
    "tsla_trailing_stop.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-30T03:16:40.869166Z",
     "start_time": "2019-10-30T03:16:39.181712Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2017-06-01    61.266599\n",
       "2017-06-02    61.266599\n",
       "2017-06-05    62.517601\n",
       "2017-06-06    63.513001\n",
       "2017-06-07    64.736999\n",
       "2017-06-08    66.600000\n",
       "2017-06-09    66.600000\n",
       "2017-06-12    66.600000\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将上述功能包装成一个函数\n",
    "def set_trailing_loss(symbol, purchase_date, perc):\n",
    "    close = pdr.DataReader(symbol, 'yahoo', start=purchase_date)['Close']\n",
    "    return close.cummax() * perc\n",
    "tsla_cummax = set_trailing_loss('tsla', '2017-06-02', .18)\n",
    "tsla_cummax.head(8)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
